/**
 * 
 */
package com.example.android_is2.pathDraw;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.StrictMode;
import android.util.Log;

/**
 * @author KAIN-PC
 * 
 */
public class XMLParser {
  // names of the XML tags
  protected static final String MARKERS = "markers";
  protected static final String MARKER = "marker";

  protected URL feedUrl;

  protected XMLParser(final String feedUrl) {
    try {
      String encodedURI = feedUrl.replaceAll(" ", "+"); // Convertire la stringa in un URL valido per API GOOGLE
      this.feedUrl = new URL(encodedURI);
    } catch (MalformedURLException e) {
      // Log.e(e.getMessage(), "XML parser - " + feedUrl);
    }
  }

  // Funzione conversione String to URL
  
  //---------------------------------------
  
  protected InputStream getInputStream() {
    try {
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);
      
      HttpURLConnection connection = (HttpURLConnection) feedUrl.openConnection();
      connection.setAllowUserInteraction(false);
      connection.setInstanceFollowRedirects(true);
      connection.setRequestMethod("GET");
      connection.setDoOutput(true);
      connection.setDoInput(true);
    //connection.connect();
      InputStream i_s = null;
      while (i_s == null)
      {
        i_s = connection.getInputStream();
      }
      return i_s;
    } catch (IOException e) {
      Log.e(e.getMessage(), "XML parser - " + feedUrl);
      return null;
    }
  }
}
